/*
 * @Description: common.js
 * @Author: zhangchenna
 * @LastEditors: Please set LastEditors
 * @Date: 2019-02-19 14:23:18
 * @LastEditTime: 2019-02-25 09:53:24
 */
export default class common {
    constructor() {
        this.scrollTop = null;
        this.bodyCls = 'modal-helper-open';
    }
    /**
     * @desc 函数防抖
     * @param func 函数
     * @param wait 延迟执行毫秒数
     * @param immediate true 表立即执行，false 表非立即执行
     */
    static debounce(func, wait, immediate) {
        let timeout;
        return function() {
            let context = this,
                args = arguments;
            if (timeout) clearTimeout(timeout);
            if (immediate) {
                let callNow = !timeout;
                timeout = setTimeout(function() {
                    timeout = null;
                }, wait);
                if (callNow) func.apply(context, args);
            } else {
                timeout = setTimeout(function() {
                    func.apply(context, args);
                }, wait);
            }
        };
    }
    /**
     * @desc 函数节流
     * @param func 函数
     * @param wait 延迟执行毫秒数
     * @param type 1 表时间戳版，2 表定时器版
     */
    static throttle(func, wait, type) {
        if (type === 1) {
            var previous = 0;
        } else if (type === 2) {
            var timeout;
        }

        return function() {
            var context = this;
            var args = arguments;
            if (type === 1) {
                var now = Date.now();

                if (now - previous > wait) {
                    func.apply(context, args);
                    previous = now;
                }
            } else if (type === 2) {
                if (!timeout) {
                    timeout = setTimeout(function() {
                        timeout = null;
                        func.apply(context, args);
                    }, wait);
                }
            }
        };
    }
    //校验手机号
    static verifyPhone(num) {
        let reg = /(^1(3[0-9]|4[5-8]|5[0-35-9]|66|8[0-9]|7[0-8]|9[89])\d{8}$)/,
            param;
        if (!reg.test(num)) {
            param = {
                type: false,
                text: '手机号格式错误'
            };
        } else {
            param = {
                type: true,
                text: '手机号格式正确'
            };
        }
        return param;
    }
    //校验中文
    static verifyChinese(value) {
        let reg = /[\u4e00-\u9fa5]+/g,
            param;
        if (!reg.test(value)) {
            param = {
                type: false,
                text: '填写内容不正确'
            };
        } else {
            param = {
                type: true,
                text: '填写内容正确'
            };
        }
        return param;
    }
    //校验身份证
    static verifyId(num) {
        let reg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/,
            param;
        if (!reg.test(num)) {
            param = {
                type: false,
                text: '身份证号格式错误'
            };
        } else {
            param = {
                type: true,
                text: '身份证号格式正确'
            };
        }
        return param;
    }
    //手机号分隔
    static phoneSeparator(value) {
        return value.replace(/\B(?=(?:\d{4})+$)/g, '-');
    }
    //防止弹出层穿透滚动
    modalHelperOpen() {
        this.scrollTop = document.scrollingElement.scrollTop;
        document.body.classList.add(this.bodyCls);
        document.body.style.top = -this.scrollTop + 'px';
    }
    modalHelperClose() {
        document.body.classList.remove(this.bodyCls);
        document.scrollingElement.scrollTop = this.scrollTop;
    }
    //IOS12 input键盘问题
    static isScroll() {
        let isScrollTop;
        isScrollTop = document.body.scrollTop;
        scrollTo(0, isScrollTop);
    }
    //格式化 YYY-MM-DD
    static formatDate(date) {
        date = new Date(Number(date) * 1000);
        let year = date.getFullYear(),
            month = date.getMonth() + 1,
            day = date.getDate();
        month = month > 10 ? month : '0' + month;
        day = day > 10 ? day : '0' + day;
        return year + '-' + month + '-' + day;
    }
    //格式化 星期
    static formatWeek(date) {
        date = new Date(Number(date) * 1000);
        let index = date.getDay(),
            week;
        switch (index) {
            case 1:
                week = '周一';
                break;
            case 2:
                week = '周二';
                break;
            case 3:
                week = '周三';
                break;
            case 4:
                week = '周四';
                break;
            case 5:
                week = '周五';
                break;
            case 6:
                week = '周六';
                break;
            case 7:
                week = '周日';
                break;
        }
        return week;
    }
    /**
     * @desc 仿jquery append
     * @param parent dom节点
     * @param text html字符串
     */
    static append(parent, text) {
        if (typeof text === 'string') {
            let temp = document.createElement('div');
            temp.innerHTML = text;
            let frag = document.createDocumentFragment();
            while (temp.firstChild) {
                frag.appendChild(temp.firstChild);
            }
            parent.appendChild(frag);
        } else {
            parent.appendChild(text);
        }
    }
    /**
     * @description: 最多两位小数
     * @param {type object} options 对象
     * @param {type number} value 数字
     * @return: void
     */
    //最多两位小数
    static floatNumSeparator(options, value) {
        let reg = /^\d+(\.\d{0,2})?$/;
        options = options || {};
        options.success =
            options.success ||
            function() {
                return false;
            };
        options.fail =
            options.fail ||
            function() {
                return false;
            };
        if (reg.test(value)) {
            options.success({
                type: true,
                text: '填写正确'
            });
        } else {
            options.fail({
                type: false,
                text: '填写数字不正确'
            });
        }
    }
    /**
     * @description:仅为正整数
     * @param {type string} type 状态
     * @param {type number} value 数字
     * @param {type function} callback 回调函数
     * @return: void
     */
    static numSeparator(type, value, callback) {
        let reg = /^[0-9]*[1-9][0-9]*$/,
            param;
        if (reg.test(value) && type == 'success') {
            param = {
                type: true,
                text: '填写正确'
            };
        } else if (!reg.test(value) && type == 'fail') {
            param = {
                type: false,
                text: '填写数字不正确'
            };
        }
        callback(param);
    }
    /**
     * @description: 类型判断
     * @param {type any} ob 任意类型
     * @return: 返回类型状态
     */
    static isType(ob) {
        return Object.prototype.toString
            .call(ob)
            .slice(8, -1)
            .toLowerCase();
    }
    /**
     * @description: 去除空格
     * @param {type string} str 字符串
     * @return: 去除空格后返回值
     */
    static trim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, '');
    }
}
